package com.view.pages.worldtown.objects
{
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Plane;

	public class Doors3D extends DisplayObject3D
	{
		public static const DOORS_CLICKED:String = "doors_clicked";
		public static const DOORS_OPENED:String = "doors_opened";
		public static const DOORS_CLOSED:String = "doors_closed";
		
		private var $doorsMC:MovieClip;
		private var $doorsBM:Bitmap;
		private var $dynamicDoorsMat:MovieMaterial;
		private var $dynamicDoors:Plane;
		private var $staticDoors:Plane;
		private var $interactiveDoors:Plane;
		private var $w:Number;
		private var $h:Number;
		
		//### CONSTRUCTOR
		public function Doors3D(_doorsMC:Class,_doorsBM:Bitmap,_w:Number,_h:Number)
		{
			$w = _w;
			$h = _h;
			$doorsBM = _doorsBM;
			var $$Doors:Class = getDefinitionByName(getQualifiedClassName(_doorsMC)) as Class;
			$doorsMC = new $$Doors();
			super();
		}
		
		//### PUBLIC METHODS
		public function start():void
		{
			
			$doorsMC.doors.gotoAndStop(0);
			var $r:Rectangle = new Rectangle(0,0,$w,$h);
			$dynamicDoorsMat = new MovieMaterial($doorsMC,true,false,false,$r);
			$dynamicDoorsMat.oneSide = false;
			$dynamicDoors = new Plane($dynamicDoorsMat,$w,$h,3,3);
			//$dynamicDoors.y = $h/2;
			
			
			//DOORS BM
			var $bmMat:BitmapMaterial = new BitmapMaterial($doorsBM.bitmapData);
			$bmMat.oneSide = false;
			$staticDoors = new Plane($bmMat,$w,$h,3,3);
			//$staticDoors.y = $h/2;
			addChild($staticDoors);
			
			//INTERACTIVE PLANE
			var $cMat:ColorMaterial = new ColorMaterial(0xFF0000,0,true);
			$cMat.oneSide = false;
			$interactiveDoors = new Plane($cMat,$w,$h,1,1);
			//$interactiveDoors.y = $h/2;
			$interactiveDoors.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK,onClick);
			$interactiveDoors.z = -30;
			addChild($interactiveDoors);
			
		}
		public function addDoorsListener():void
		{
			$doorsMC.doors.addEventListener("opened",onOpen);
			$doorsMC.doors.addEventListener("closed",onClose);
		}
		public function removeDoorsListener():void
		{
			$doorsMC.doors.removeEventListener("opened",onOpen);
			$doorsMC.doors.removeEventListener("closed",onClose);
		}
		public function open():void
		{
			removeChild($staticDoors);
			addChild($dynamicDoors);
			$dynamicDoorsMat.animated = true;
			$doorsMC.doors.gotoAndPlay("open");
		}
		public function close():void
		{
			removeChild($staticDoors);
			addChild($dynamicDoors);
			$dynamicDoorsMat.animated = true;
			$doorsMC.doors.gotoAndPlay("close");
		}
		public function remove():void
		{
			addChild($staticDoors);
			removeChild($dynamicDoors);
			addChild($interactiveDoors);
			$dynamicDoorsMat.animated = false;
			$doorsMC.doors.gotoAndStop("open");
		}
		//### LISTENERS
		private function onClick(_e:InteractiveScene3DEvent):void
		{
			dispatchEvent(new Event(DOORS_CLICKED));
		}
		private function onOpen(_e:Event):void
		{
			removeChild($dynamicDoors);
			removeChild($interactiveDoors);
			dispatchEvent(new Event(DOORS_OPENED));
		}
		private function onClose(_e:Event):void
		{
			addChild($staticDoors);
			removeChild($dynamicDoors);
			addChild($interactiveDoors);
			$dynamicDoorsMat.animated = false;
			dispatchEvent(new Event(DOORS_CLOSED));
			$doorsMC.doors.gotoAndStop("open");
		}
		private function addDoorsClick():void
		{
			$interactiveDoors.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK,onClick);
		}
		private function removeDoorsClick():void
		{
			$interactiveDoors.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK,onClick);
		}
	}
}